package com.dyh.quickdemo.bizDemo.syncWarehouseToWms;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

/*
@Author=Joey Wheeler
@Date=2020/12/29 16:16
*/
@Slf4j
public class WmsWarehouseBiz {

    public static String pushWarehouse(String code){
        if (code.length()>4)
            throw new RuntimeException("推送仓库异常");
       // log.info("推送仓库成功");
        return code;
    }

    public  static void pushLShipping(String code){
        Random random = new Random();
        if (random.nextInt(2)<1)
            throw new RuntimeException("推送l快递异常");
       // log.info("推送l快递成功");

    }

    public  static void pushLShipping(){
        if (1==1)
            throw new RuntimeException("推送l快递异常");
       // log.info("推送l快递成功");

    }

    public static void pushPShipping(){
      //  log.info("推送p快递成功");

    }

    public static void main(String[] args) {
        fun();
    }


    /*
    15:50:09.675 [main] ERROR com.dyh.quickdemo.bizDemo.syncWarehouseToWms.WmsWarehouseBiz - 异常e=
java.util.concurrent.CompletionException: java.lang.RuntimeException: 推送l快递异常
	at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
	at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
	at java.util.concurrent.CompletableFuture.uniRun(CompletableFuture.java:708)
	at java.util.concurrent.CompletableFuture.uniRunStage(CompletableFuture.java:717)
	at java.util.concurrent.CompletableFuture.thenRun(CompletableFuture.java:2010)
	at com.dyh.quickdemo.bizDemo.syncWarehouseToWms.WmsWarehouseBiz.fun(WmsWarehouseBiz.java:53)
	at com.dyh.quickdemo.bizDemo.syncWarehouseToWms.WmsWarehouseBiz.main(WmsWarehouseBiz.java:45)
Caused by: java.lang.RuntimeException: 推送l快递异常
	at com.dyh.quickdemo.bizDemo.syncWarehouseToWms.WmsWarehouseBiz.pushLShipping(WmsWarehouseBiz.java:34)
	at java.util.concurrent.CompletableFuture.uniRun(CompletableFuture.java:705)
	... 4 common frames omitted
    */
    public static void fun(){
        String code = "1141";


        CompletableFuture.supplyAsync(()->pushWarehouse(code))
                .thenRun(WmsWarehouseBiz::pushLShipping)
//                .exceptionally(e->{
//                    log.error("异常e1=",e);
//                    return null;
//                })
                .thenRun(WmsWarehouseBiz::pushPShipping)
                .exceptionally(e->{
                    log.error("异常e2=",e);
                    return null;
                })
                .join()
        ;
    }

    @Test
    public void test(){

        String code = "1141";
        CompletableFuture cf = CompletableFuture.supplyAsync(()->pushWarehouse(code))
                .thenRun(WmsWarehouseBiz::pushLShipping)
                .thenRun(WmsWarehouseBiz::pushPShipping);



    }
}
